package org.hdax.dm.service.impl;

import org.hda.dm.utils.DateUtil;
import org.hdax.dm.client.DmSchedulerClient;
import org.hdax.dm.client.DmSchedulerSeatClient;
import org.hdax.dm.client.DmSchedulerSeatPriceClient;
import org.hdax.dm.entity.scheduler.DmScheduler;
import org.hdax.dm.entity.scheduler_seat.DmSchedulerSeat;
import org.hdax.dm.entity.scheduler_seat_price.DmSchedulerSeatPrice;
import org.hdax.dm.service.DmSchedulerService;
import org.hdax.dm.vo.CommonResponse;
import org.hdax.dm.vo.VoUtil;
import org.hdax.dm.vo.entity.scheduler.VoScheduler;
import org.hdax.dm.vo.entity.scheduler_seat_price.VoSchedulerSeatPrice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DmSchedulerServiceImpl implements DmSchedulerService {
    @Autowired
    private DmSchedulerClient dmSchedulerClient;

    @Autowired
    private DmSchedulerSeatPriceClient dmSchedulerSeatPriceClient;

    @Autowired
    private DmSchedulerSeatClient dmSchedulerSeatClient;

    @Override
    public CommonResponse<List<VoScheduler>> queryItemScheduler(Integer itemId) {
        List<VoScheduler> voSchedulerList = new ArrayList<>();
        DmScheduler dmScheduler = new DmScheduler();
        dmScheduler.setItemId(itemId);
        List<DmScheduler> dmSchedulerList = dmSchedulerClient.findByDmScheduler(dmScheduler);

        dmSchedulerList.forEach(item -> {
            VoScheduler voScheduler = new VoScheduler();
            BeanUtils.copyProperties(item, voScheduler);
            voScheduler.setStartTime(DateUtil.dateToString(item.getStartTime()));
            voScheduler.setEndTime(DateUtil.dateToString(item.getEndTime()));
            voSchedulerList.add(voScheduler);
        });
        return VoUtil.returnSuccess(voSchedulerList);
    }

    @Override
    public CommonResponse<List<VoSchedulerSeatPrice>> queryItemPrice(Integer scheduleId) {
        List<VoSchedulerSeatPrice> voSchedulerSeatPriceList = new ArrayList<>();
        DmSchedulerSeatPrice dmSchedulerSeatPrice = new DmSchedulerSeatPrice();
        dmSchedulerSeatPrice.setScheduleId(scheduleId);
        List<DmSchedulerSeatPrice> dmSchedulerSeatPriceList = dmSchedulerSeatPriceClient.findByDmSchedulerSeatPrice(dmSchedulerSeatPrice);

        dmSchedulerSeatPriceList.forEach(item -> {
            VoSchedulerSeatPrice voSchedulerSeatPrice = new VoSchedulerSeatPrice();
            BeanUtils.copyProperties(item, voSchedulerSeatPrice);
            //判断数据库中已存在的
            voSchedulerSeatPrice.setIsHaveSeat(dmSchedulerSeatClient.findByDmSchedulerSeatCount(new DmSchedulerSeat(item.getScheduleId(), 1, item.getAreaLevel())) > 0 ? 1 : 0);
            voSchedulerSeatPriceList.add(voSchedulerSeatPrice);
        });
        return VoUtil.returnSuccess(voSchedulerSeatPriceList);
    }
}
